<template>
  <div class="add-edit-notice-view" v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item :label="$t('tables_title.title')" prop="title">
        <el-input v-model="ruleForm.title" :placeholder="$t('tables_title.title')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.content')" prop="content">
        <div class="content-view">
          <div id="editor"></div>
        </div>
      </el-form-item>
      <div style="text-align: center;">
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t('global.submit')}}</el-button>
        <el-button @click="cancelView">{{$t('global.cancel')}}</el-button>
      </div>
    </el-form>

  </div>
</template>

<script>

import E from 'wangeditor'

import { addNotice,updateNotice } from '@/api/noticeApi.js'

export default {
  data(){
    return{
      loading:false,
      ruleForm:{
        title:'',
        content:'',
      },
      rules:{
          title: [
            { required: true, message: this.$t('notice_management.empty_title'), trigger: 'blur' },
          ],
          content: [
            { required: true, message: this.$t('notice_management.empty_content'), trigger: 'blur' },
          ]
      },
      editor: null,
      info_: null
    }
  },
  props: ['editNoticeInfo'],
  watch:{
    editNoticeInfo(newVal,oldVal){
      this.ruleForm = newVal
      console.log(this.ruleForm.content)
      if(this.ruleForm.content == undefined){
        console.log(this.ruleForm.content)
        this.editor.txt.html('')

      }else{
        this.editor.txt.html(this.ruleForm.content)
      }

    }
  },
  components:{

  },
  created(){
    this.ruleForm = this.editNoticeInfo
  },
  mounted(){
    //wangeditor “＞=3.0.0“时会出现这个错误，当前是4.7.7版本，貌似是版本问题，用this.editor.customConfig = this.editor.customConfig ? this.editor.customConfig : this.editor.config; 兼容老版本。
    this.editor = new E('#editor')
    // 关闭粘贴样式的过滤
    this.editor.customConfig = this.editor.customConfig ? this.editor.customConfig : this.editor.config;
    this.editor.customConfig.uploadImgShowBase64 = false // base 64 存储图片
    this.editor.customConfig.uploadImgServer = 'http://otp.cdinfotech.top/file/upload_images'// 配置服务器端地址
    this.editor.customConfig.uploadImgHeaders = { }// 自定义 header
    this.editor.customConfig.uploadFileName = 'file' // 后端接受上传文件的参数名
    this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
    this.editor.customConfig.uploadImgMaxLength = 6 // 限制一次最多上传 3 张图片
    this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000 // 设置超时时间
    this.editor.customConfig.onchange = (html) => {
      this.ruleForm.content = html // 绑定当前逐渐地值
    }
    this.editor.create()
    // 改变富文本内部的值
    this.editor.txt.html(this.ruleForm.content)
  },
  methods:{
    submitForm(formName) {
      console.log(this.ruleForm.content)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.ruleForm.content == undefined || this.ruleForm.content == ''){
            // this.$message.error('请输入通知内容')
          }else{
            if(this.ruleForm.noticeId){
              this.userEditNotice()
            }else{
              this.userAddBotice()
            }
          }
        } else {
          return false;
        }
      });
    },
    userEditNotice(){         //修改Notice信息
      this.loading = true
      updateNotice(this.ruleForm).then((res)=>{
        this.loading = false
        console.log(res)
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.modified_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    userAddBotice(){          //新增Notice
      this.loading = true
      addNotice(this.ruleForm).then((res)=>{
        this.loading = false
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.added_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    cancelView(){       //关闭弹窗
      this.$emit('cancelView')
    },
    refreshData(){
      this.$emit('refreshData')
    }
  }
}
</script>

<style lang="scss" scoped>
.add-edit-notice-view{
  padding-right: 30px;
  padding-left: 30px;
  height: 600px;
}

.content-view{
  width: 100%;
  height: 470px;
}
</style>
